package com.example.Android_test.activity.training;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.example.Android_test.activity.Main;
import com.example.Android_test.helpers.SettingsHelper;
import com.example.Android_test.util.PlaySound;
import com.example.Android_test.util.SimpleTimer;

/**
 * Created with IntelliJ IDEA.
 * User: anpi0413
 * Date: 30.05.13
 * Time: 18:36
 * To change this template use File | Settings | File Templates.
 */
public abstract class Abstract_Training extends Activity {
    private static final int HOW_MANY_SECOND_IS_SLEEP = 6;

    private SimpleTimer timer;
    private int idleCounter = 0;

    protected abstract void onFirstRun();
    protected abstract void next();
    protected abstract String getKeyTrainingTimeSpent();

    protected void trainingStart() {
        timer.startTimer();
    }

    protected void trainingStop() {
        timer.stopTimer();
    }

    protected void playSound(String soundResName) {
        new PlaySound(soundResName, true).play();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) { // The activity is being created.
        super.onCreate(savedInstanceState);

        timer = new SimpleTimer();
        timer.addStepListener(new StepSecondActionListener());
        //timer.startTimer();
    }
    @Override
    protected void onStart() { // The activity is about to become visible.
        super.onStart();

    }
    @Override
    protected void onResume() { // The activity has become visible (it is now "resumed").
        super.onResume();

        timer.startTimer();
    }
    @Override
    protected void onPause() {// Another activity is taking focus (this activity is about to be "paused").
        super.onPause();

        timer.stopTimer();
    }
    @Override
    protected void onStop() { // The activity is no longer visible (it is now "stopped")
        super.onStop();

        timer.stopTimer();
    }
    @Override
    protected void onDestroy() { // The activity is about to be destroyed.
        super.onDestroy();

        timer.stopTimer();
        timer.deleteTimer();
    }

    @Override
    public void onUserInteraction()
    {
        super.onUserInteraction();

        idleCounter = 0;
        timer.startTimer();
    }


    private class StepSecondActionListener implements SimpleTimer.ActionListener {
        @Override
        public void actionPerformed(Object o) {
            Integer currentSecond = Integer.parseInt(SettingsHelper.INSTANCE.getStringPreference(getKeyTrainingTimeSpent()));
            SettingsHelper.INSTANCE.setStringPreference(getKeyTrainingTimeSpent(), String.valueOf(currentSecond + 1));

            idleCounter++;
            if (idleCounter >= HOW_MANY_SECOND_IS_SLEEP) {
                timer.stopTimer();
            }

            Log.d(Main.LOG_TAG, "+1 second");
        }
    }
}
